package com.example.dllo.my36kr.ui.activity.sonactivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.SearchBean;
import com.example.dllo.my36kr.net.SingleVolley;
import com.example.dllo.my36kr.net.VolleyResult;
import com.example.dllo.my36kr.tools.SearchUrls;
import com.example.dllo.my36kr.ui.activity.AbsBaseActivity;
import com.example.dllo.my36kr.ui.adapter.SearchAdapter;
import com.example.dllo.my36kr.ui.adapter.SearchAllAdapter;
import com.google.gson.Gson;

import java.util.List;


/**
 * 搜索的页面
 * Created by CPC_272 on 16/8/22.
 */
public class SearchActivity extends AbsBaseActivity implements View.OnClickListener, VolleyResult {
    private EditText searchEt;
    private ImageButton searchImgBtn;
    private ImageView searchGroudImg,searchRemove;
    private TextView searchCancle, searchNull;
    private ListView searchLV;
    private LinearLayout more_layout;
    @Override
    protected int setLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        searchGroudImg = byView(R.id.search_background_img);//背景图片
        searchEt = byView(R.id.search_edit); //editText
        searchNull = byView(R.id.search_null);//无搜索记录
        searchImgBtn = byView(R.id.search_Btn);//搜索
        searchCancle = byView(R.id.search_cancle_tv);//取消
        searchRemove = byView(R.id.remove_editText_img);//X
        searchLV = byView(R.id.ls_search);
        more_layout = byView(R.id.more_layout);
    }
    @Override
    protected void initData() {
        more_layout.setOnClickListener(this);
        searchImgBtn.setOnClickListener(this);
        searchRemove.setOnClickListener(this);
        searchCancle.setOnClickListener(this);
        //EditText的监听事件 需要newTextWatcher这个
        searchEt.addTextChangedListener(new TextWatcher() {

            // //text  输入框中改变前的字符串信息
            //start 输入框中改变前的字符串的起始位置
            //count 输入框中改变前后的字符串改变数量一般为0
            //after 输入框中改变后的字符串与起始位置的偏移量
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            //text  输入框中改变后的字符串信息
            //start 输入框中改变后的字符串的起始位置
            //before 输入框中改变前的字符串的位置 默认为0
            //count 输入框中改变后的一共输入字符串的数量
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                   //判断 editText不为空的时候执行
                if (!TextUtils.isEmpty(s)) {
                     searchRemove.setVisibility(View.VISIBLE);
                     more_layout.setVisibility(View.VISIBLE);
                    String keyWords = searchEt.getText().toString();
                    String searchUrl = SearchUrls.searchTop + keyWords + SearchUrls.searchEnd;
                    SingleVolley.getInstence(SearchActivity.this).startRequest(searchUrl, SearchActivity.this,1);
                    searchGroudImg.setVisibility(View.GONE);
                    searchNull.setVisibility(View.GONE);
                    searchLV.setVisibility(View.VISIBLE);
                    //当editText为空的时候效果
                }else {
                    searchRemove.setVisibility(View.GONE);
                    more_layout.setVisibility(View.GONE);
                    searchGroudImg.setVisibility(View.VISIBLE);
                    searchNull.setVisibility(View.VISIBLE);
                   searchLV.setVisibility(View.GONE);
                }
            }
            //edit  输入结束呈现在输入框中的信息
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_cancle_tv://返回
                 finish();
                break;
            case R.id.remove_editText_img: //删除editText上输入的字符
                searchEt.setText("");
                searchRemove.setVisibility(View.GONE);
                searchGroudImg.setVisibility(View.VISIBLE);
                searchNull.setVisibility(View.VISIBLE);
                more_layout.setVisibility(View.GONE);
                break;
            case  R.id.search_Btn://搜索
                String  keyWords = searchEt.getText().toString();
                String searchUrl = SearchUrls.searchTop + keyWords + SearchUrls.searchEnd;
                SingleVolley.getInstence(this).startRequest(searchUrl, this,1);
                searchGroudImg.setVisibility(View.GONE);
                searchNull.setVisibility(View.GONE);
                searchLV.setVisibility(View.VISIBLE);
                break;
            case  R.id.more_layout://搜索更多
                String  keyWords1 = searchEt.getText().toString();
                String searchUrl1 = SearchUrls.searchTop + keyWords1 + SearchUrls.searchEnd;
                SingleVolley.getInstence(this).startRequest(searchUrl1, this,2);
                searchGroudImg.setVisibility(View.GONE);
                searchNull.setVisibility(View.GONE);
                searchLV.setVisibility(View.VISIBLE);
                break;


        }
    }

    @Override
    public void success(String str, int type) {
        switch (type) {
            case 1:
                Gson gson = new Gson();
                SearchBean searchBean = gson.fromJson(str, SearchBean.class);
                SearchBean.DataBean dataBean = searchBean.getData();
                List<SearchBean.DataBean.DataBean1> dataBean1s = dataBean.getData();
                SearchAdapter searchAdapter = new SearchAdapter(this);
                searchAdapter.setDataBean1s(dataBean1s);
                searchLV.setAdapter(searchAdapter);
                break;
            case 2:
                Gson gson1 = new Gson();
                SearchBean searchBean1 = gson1.fromJson(str, SearchBean.class);
                SearchBean.DataBean dataBean1 = searchBean1.getData();
                List<SearchBean.DataBean.DataBean1> dataBean1s1 = dataBean1.getData();
                SearchAllAdapter searchAdapter1 = new SearchAllAdapter(this);
                searchAdapter1.setDataBean1s(dataBean1s1);
                searchLV.setAdapter(searchAdapter1);
        }
    }

    @Override
    public void success(String str) {

    }

    @Override
    public void failure() {

    }
}
